package cloud.simple.zuul.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import cloud.simple.zuul.model.User;
import rx.Observable;

@Service
public class AggregationService {

	@Autowired
	private RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "fallback")
	public Observable<User> getUserId(Long id) {
		return Observable.create(observer -> {
			User user = restTemplate.getForObject("http://cloud-simple-service/getUserById/{id}", User.class, id);
			observer.onNext(user);
			observer.onCompleted();
		});
	}

	@HystrixCommand(fallbackMethod = "fallback")
	public Observable<User> getUiUserByUserId(Long id) {
		return Observable.create(observer -> {
			User user = restTemplate.getForObject("http://cloud-simple-ui/getUserById/{id}", User.class, id);
			observer.onNext(user);
			observer.onCompleted();
		});
	}

	public User fallback(Long id) {
		User user = new User();
		user.setId(-1L);
		return user;
	}
}
